package cn.parllay.dingdangproject.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import java.lang.reflect.Method;

import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.service.MyNoticeService;
import cn.parllay.dingdangproject.utils.SpUtils;
import cn.parllay.dingdangproject.utils.aliyun.PushManager;

import static cn.parllay.dingdangproject.DingDangApplication.getContext;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        //开启VIP提醒服务
        startNoticeServer();

    }


    @OnClick(R.id.tv_logout)
    void onLogoutClick(){
        logout();
    }

    @OnClick(R.id.tv_showVip)
    void onShowVipClick(){
        Intent intent = new Intent(this, VipCustomerActivity.class);
        startActivity(intent);
    }


    private void logout() {
        //清除登录状态
        SpUtils.getInstance(getContext()).save(SpUtils.IS_LOGIN, false);

        //解绑阿里推送
        PushManager.getInstance().unBindPush();

        finish();
    }

    private void startNoticeServer() {

        if (Build.VERSION.SDK_INT >= 23 && !commonROMPermissionCheck(this)) {
            new AlertDialog.Builder(this)
                    .setCancelable(false)
                    .setTitle("需要开启悬浮窗权限")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            requestAlertWindowPermission();
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();

        } else {
            //绑定阿里移动推送
            PushManager.getInstance().bindPush();
        }

    }

    private static final int REQUEST_CODE = 1;

    //判断权限
    private boolean commonROMPermissionCheck(Context context) {
        Boolean result = true;
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                Class clazz = Settings.class;
                Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
                result = (Boolean) canDrawOverlays.invoke(null, context);
            } catch (Exception e) {
                Log.e(TAG, Log.getStackTraceString(e));
            }
        }
        return result;
    }

    //申请权限
    private void requestAlertWindowPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, REQUEST_CODE);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
//处理回调
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE) {
            if (Settings.canDrawOverlays(this)) {
                //绑定阿里移动推送
                PushManager.getInstance().bindPush();
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //停止服务
//        stopNoticeServer();
    }

    private void stopNoticeServer() {
        Log.e(TAG, "停止服务");
        Intent intent = new Intent(this, MyNoticeService.class);
        stopService(intent);
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }
}
